<template>
  <div class="app-container home">


    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">


      <el-row>
        <el-col :span="24">
          <el-form-item label="时间区间">
            <el-radio-group size="small" v-model="timeType" @change="onChangeGroup">
              <el-radio-button v-for="(option, index) in options" :key="index"
                               :label="option.value">{{option.label}}
              </el-radio-button>
            </el-radio-group>

            <el-date-picker
              size="small"
              value-format="yyyy-MM-dd"
              style="margin-left: 10px"
              v-if="showSelectData"
              v-model="value1"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>


      <el-form-item label="媒体名称" prop="mediaName">
        <el-input
          v-model="queryParams.mediaName"
          placeholder="请输入媒体名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="作者" prop="mediaName">
        <el-input
          v-model="queryParams.selfMediaAccount"
          placeholder="作者名字或自媒体名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="媒体类型" prop="mediaType">
        <el-select v-model="queryParams.mediaType" placeholder="请选择媒体类型" clearable size="small">
          <el-option
            v-for="dict in mediaTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictLabel"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="情感特征" >
        <el-select v-model="selectEmotion" placeholder="请选择媒体类型" clearable size="small">
          <el-option
            v-for="(dict,index) in emotionalCharacteristicsOptions"
            :key="index"
            :value="dict.dictValue"
            :label="dict.dictLabel"
          >
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="关键词" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入关键词"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:opinion:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:opinion:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="opinionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="发布日期" align="center" prop="publishTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d} {h}:{mi}:{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="情感特征" align="center" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.ispositive === 1" style="color: green"> 正面
          </div>
          <div v-else-if="scope.row.isnegative === 1" style="color:red"> 负面
          </div>
          <div v-else-if="scope.row.isneutral === 1" style="color: grey"> 中性
          </div>
          <div v-else style="background-color: #909399"> 无
          </div>
        </template>
      </el-table-column>
      <el-table-column label="标题/内容">
        <template slot-scope="scope">

          <div
            class="article-title"
            @click="GotoDetail(scope.row.id)"
            v-html="scope.row.title!=null?scope.row.title:scope.row.content"
          >
          </div>


        </template>

      </el-table-column>
      <el-table-column label="媒体名称" align="center" prop="siteName" width="200"/>
<!--      <el-table-column label="媒体类型" align="center" prop="carrie" width="100"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:opinion:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import {
    queryListOpinion,
    delOpinion,
    exportOpinion
  } from "@/api/system/opinion";

  export default {
    name: "Opinion",
    components: {},
    data() {
      return {
        selectEmotion: '',
        showSelectData: false,
        value1: [],
        timeType: 'all',
        options: [
          {label: '全部', value: 'all'},
          {label: '今天', value: 'today'},
          {label: '昨天', value: 'yesterday'},
          {label: '近7天', value: 'week'},
          {label: '近30天', value: 'month'},
          {label: '自定义', value: 'define'},
        ],
        emotionalCharacteristicsOptions: [],
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 舆情表格数据
        opinionList: [],


        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          mediaName: null,
          mediaType: null,

          keywords: null,
          title: null,
          comments: null,
          url: null,
          publishDate: null,
          publishDay: null,
          publishHour: null,
          selfMediaAccount: null,
          summary: null,
          area: null,
          relateArea: null,
          industry: null,
          forward: null,
          accountType: null,
          commentBloggers: null,
          insertBy: null,
          insertAt: null,
          updateAt: null,

        },
        parameter: {
          timeType: this.timeType,
          startDateTime: "",
          endDateTime: ""
        },


        mediaTypeOptions: []
      };
    },
    created() {
      this.timeType = this.$route.query.timeType
      if(this.$route.query.sensitiveInformation!=null){
        if(this.$route.query.sensitiveInformation==='正面'){
            this.selectEmotion='positive';
        }else if(this.$route.query.sensitiveInformation==='中性'){
          this.selectEmotion='neutral';
        }else if(this.$route.query.sensitiveInformation==='负面'){
          this.selectEmotion='negative';
        }
      }
      this.queryParams.mediaType = this.$route.query.mediaType
      this.queryParams.keywords = this.$route.query.keywords
      this.queryParams.mediaName = this.$route.query.mediaName
      this.queryParams.selfMediaAccount = this.$route.query.author

      if (this.$route.query.beginPublishDay != null) {
        this.parameter.startDateTime = this.$route.query.beginPublishDay
        let startDateTime = this.$route.query.beginPublishDay
        this.showSelectData = true;
        this.value1.push(startDateTime)
      }
      if (this.$route.query.endPublishDay != null) {
        this.parameter.endDateTime = this.$route.query.endPublishDay
        let endDateTime = this.$route.query.endPublishDay
        this.showSelectData = true;
        this.value1.push(endDateTime)
      }
      this.getList();
      this.getDicts("media_type").then(response => {
        this.mediaTypeOptions = response.data;
      });
      this.getDicts("emotional_characteristics").then(response => {
        this.emotionalCharacteristicsOptions = response.data;
      });
    },
    methods: {
      GotoDetail(id) {
        if (id == null) {
          this.$message({showClose: true, message: "该舆情ID为空", type: "warning"});
          return
        }
        this.$router.push({path: '/opinion/detail', query: {id: id}});
      },
      onChangeGroup() {
        this.showSelectData = this.timeType === "define" ? true : false
        if (this.timeType !== "define") {

        }
      },
      /** 查询舆情列表 */
      getList() {
        this.loading = true;
        this.parameter.timeType = this.timeType;
        if (this.value1 != null && this.value1.length !== 0) {
          this.parameter.startDateTime = this.value1[0]
          this.parameter.endDateTime = this.value1[1]
        }

        if (this.selectEmotion === 'positive') {
          this.parameter.ispositive = 1
          this.parameter.isneutral = null
          this.parameter.isnegative = null
        } else if (this.selectEmotion === 'neutral') {
          this.parameter.ispositive = null
          this.parameter.isneutral = 1
          this.parameter.isnegative = null
        } else if (this.selectEmotion === 'negative') {
          this.parameter.ispositive = null
          this.parameter.isneutral = null
          this.parameter.isnegative = 1
        } else {
          this.parameter.ispositive = null
          this.parameter.isneutral = null
          this.parameter.isnegative = null
        }
        console.log(JSON.stringify(this.parameter))
        queryListOpinion(this.queryParams, this.parameter).then(response => {
          this.opinionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },


      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },



      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除舆情编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delOpinion(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有舆情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOpinion(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
      }
    }
  };
</script>

<style scoped lang="scss">
  .home {
    background-color: white;

    .article-title {
      font-size: 14px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      display: inline-block;
      width: 100%;
      color: #1e1e1e
    }
  }
</style>
